package com.vm.activity;

import java.util.Timer;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;
import com.vm.commons.CurrentUser;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.utils.MyTimerTask;
import com.vm.utils.MyVolley;
import com.vm.utils.NumberService;
import com.wwtscapp.apk.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class RechargePhoneActivity extends Activity {
	ImageButton imgbtn_search_back;
	TextView tv_integral;
	RadioButton rbtn_ten;
	RadioButton rbtn_thirty;
	RadioButton rbtn_hundred;
	EditText et_phone;
	EditText et_code;
	Button btn_get_verification_code;
	Button btn_next;
	private RequestQueue mQueue;
	private RequestQueue rQueue;
	String phone = CurrentUser.PHONNE;
	Timer timer;
	int countDown = 90;
	String code;
	String number;
	static String loacation;
	private MyTimerTask mTimerTask;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exchange);
		mQueue = MyVolley.getRequestQueue();
		rQueue = MyVolley.getRequestQueue();
		init();
	}

	public void init() {
		imgbtn_search_back = (ImageButton) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		tv_integral = (TextView) findViewById(R.id.tv_integral);
		rbtn_ten = (RadioButton) findViewById(R.id.rbtn_ten);
		rbtn_thirty = (RadioButton) findViewById(R.id.rbtn_thirty);
		rbtn_hundred = (RadioButton) findViewById(R.id.rbtn_hundred);
		et_phone = (EditText) findViewById(R.id.et_phone);
		et_phone.setText(phone);
		et_phone.setEnabled(false);
		et_phone.setFocusable(false);
		et_code = (EditText) findViewById(R.id.et_code);
		btn_get_verification_code = (Button) findViewById(R.id.btn_get_verification_code);
		btn_next = (Button) findViewById(R.id.btn_next);
		tv_integral.setText(Integer.toString(CurrentUser.INTERGRAL));
		btn_get_verification_code.setOnClickListener(getCode_Listener);
		btn_next.setOnClickListener(recharge_Listener);
	}

	OnClickListener recharge_Listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			code = et_code.getText().toString();
			if (rbtn_ten.isChecked()) {
				number = "30";
			} else if (rbtn_thirty.isChecked()) {
				number = "50";
			} else if (rbtn_hundred.isChecked()) {
				number = "100";
			} else {
				Toast toast = Toast.makeText(RechargePhoneActivity.this,
						"请选择充值金额", 500);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
			if (CurrentUser.INTERGRAL > (Integer.parseInt(number) * 100)) {
				if (code == null || code.equals("")) {
					Toast toast = Toast.makeText(RechargePhoneActivity.this,
							"请输入验证码", 500);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else {
					String url = VMConst.REQUEST_URL + "checkCode?phone="
							+ phone + "&code=" + code;
					StringRequest stringRequest = new StringRequest(url,
							new Listener<String>() {
								@Override
								public void onResponse(String arg0) {
									// TODO Auto-generated method stub
									if (arg0.contains("正确")) {
										String urlString = VMConst.REQUEST_URL
												+ "addRecharge?userId="
												+ CurrentUser.USERID
												+ "&phone=" + phone
												+ "&number=" + number;
										StringRequest request = new StringRequest(
												urlString,
												new Listener<String>() {

													@Override
													public void onResponse(
															String arg0) {
														// TODO Auto-generated
														// method stub
														if (arg0.equals("1")) {
															Toast toast = Toast
																	.makeText(
																			RechargePhoneActivity.this,
																			"提交成功,话费会在24小时内到账",
																			500);
															toast.setGravity(
																	Gravity.CENTER,
																	0, 0);
															toast.show();
															CurrentUser.INTERGRAL = CurrentUser.INTERGRAL
																	- Integer
																			.parseInt(number)
																	* 100;
															tv_integral
																	.setText(Integer
																			.toString(CurrentUser.INTERGRAL));
															et_phone.setText("");
															et_code.setText("");
															et_phone.setEnabled(true);
														} else {
															Toast toast = Toast
																	.makeText(
																			RechargePhoneActivity.this,
																			"充值失败",
																			500);
															toast.setGravity(
																	Gravity.CENTER,
																	0, 0);
															toast.show();
															et_phone.setEnabled(true);
														}
													}

												}, null);
										rQueue.add(request);
									} else {
										Toast toast = Toast.makeText(
												RechargePhoneActivity.this,
												"验证码错误", 500);
										toast.setGravity(Gravity.CENTER, 0, 0);
										toast.show();
									}

								}
							}, null);
					mQueue.add(stringRequest);
				}
			} else {
				Toast toast = Toast.makeText(RechargePhoneActivity.this,
						"积分不足", 500);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		}

	};
	OnClickListener getCode_Listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			phone = et_phone.getText().toString();
			if (rbtn_ten.isChecked()) {
				number = "30";
			} else if (rbtn_thirty.isChecked()) {
				number = "50";
			} else if (rbtn_hundred.isChecked()) {
				number = "100";
			} else {
				Toast toast = Toast.makeText(RechargePhoneActivity.this,
						"请选择充值金额", 500);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
			if (phone == null || phone.equals("")) {
				Toast toast = Toast.makeText(RechargePhoneActivity.this,
						"请输入手机号", 500);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else if (Integer.parseInt(number) * 100 > CurrentUser.INTERGRAL) {
				Toast toast = Toast.makeText(RechargePhoneActivity.this,
						"积分不足", 500);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else {
				// newThread.start();
				btn_get_verification_code.setEnabled(false);
				timer = new Timer(true);
				mTimerTask = new MyTimerTask(countDown, handler);
				timer.schedule(mTimerTask, 1000, 1000);
				if (phone != null) {
					String url = VMConst.REQUEST_URL
							+ "getMobileCode?mobilephone="
							+ et_phone.getText().toString() + "&deviceId="
							+ StringUtil.DEVICEID;
					StringRequest stringRequest = new StringRequest(url,
							new Listener<String>() {

								@Override
								public void onResponse(String arg0) {
									// TODO Auto-generated
									// method stub
									if (arg0.contains("1")) {
										Toast toast3 = Toast.makeText(
												RechargePhoneActivity.this,
												"正在发送，请稍后", 500);
										toast3.setGravity(Gravity.CENTER, 0, 0);
										toast3.show();
									}
								}
							}, null);
					mQueue.add(stringRequest);
					btn_get_verification_code.setEnabled(false);
				} else {
					Toast toast = Toast.makeText(RechargePhoneActivity.this,
							"请绑定手机号", 500);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
			}
		}

	};

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				String remainTime = msg.obj.toString();
				btn_get_verification_code.setText("重新发送" + remainTime);
				break;

			case 2:
				btn_get_verification_code.setText("重新发送");
				btn_get_verification_code.setEnabled(true);
				timer.cancel();
				break;
			case 3:
				Toast toast3 = Toast.makeText(RechargePhoneActivity.this,
						loacation, 500);
				toast3.setGravity(Gravity.CENTER, 0, 0);
				toast3.show();
				String url = VMConst.REQUEST_URL + "getMobileCode?mobilephone="
						+ et_phone.getText().toString() + "&deviceId="
						+ StringUtil.DEVICEID;
				StringRequest stringRequest = new StringRequest(url,
						new Listener<String>() {

							@Override
							public void onResponse(String arg0) {
								// TODO Auto-generated
								// method stub
								if (arg0.contains("1")) {
									Toast toast3 = Toast.makeText(
											RechargePhoneActivity.this,
											"正在发送，请稍后", 500);
									toast3.setGravity(Gravity.CENTER, 0, 0);
									toast3.show();
								}
							}
						}, null);
				mQueue.add(stringRequest);
				btn_get_verification_code.setEnabled(false);
				// 发送验证码后禁止输入手机号码
				et_phone.setEnabled(false);
				break;
			case 4:
				if (number.length() == 11) {

				} else {
					Toast toast = Toast.makeText(RechargePhoneActivity.this,
							"手机号输入错误", 500);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
				break;
			}
			super.handleMessage(msg);
		}
	};

	Thread newThread = new Thread(new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				NumberService service = new NumberService();
				loacation = service.getLocation(phone);
				Message message = new Message();
				message.what = 3;
				handler.sendMessage(message);
			} catch (Exception e) {
				e.printStackTrace();
				loacation = "号码不存在";
				Message message = new Message();
				message.what = 4;
				handler.sendMessage(message);
			}
		}

	});

}